package com.lkm.app_ui.m.Imple.hsj522;

import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Looper;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.help.UsbCDCConnectTask;
import com.lkm.app_ui.m.nfc.OnNFCModuleStateEvent;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.task.ProgressData;
import com.lkm.frame.task2.Task;
import com.lkm.hardware_connect.AbsOnDeviceStateEvent;
import com.lkm.hardware_connect.ConnectTaskResult;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.JsonP;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.netapi.Api;
import com.lkm.usb_com.connect.USBConnectImple;

import java.util.HashMap;
import java.util.Iterator;



public class CardCircuitCom extends USBConnectImple{

    @Override
    protected Task<IHardwareControler, ?, ?> createReadTask(IHardwareControler hardwareControler) {
        return new ReadTask(this,new DefaultTaskExecutor(AppConst.getTaskExecutor(),true),AppConst.getTaskManager2());
    }

    public CardCircuitCom() {
        super(AppConst.getApplication(), AppConst.getTaskManager2(), Looper.getMainLooper(), AppConst.getTaskExecutor());
    }

    @Override
    protected boolean equalsUsbDevice(UsbDevice usbDevice) {
        return usbDevice.getProductId() == 0x6001 && usbDevice.getVendorId() == 0x0403;
    }

    private UsbDevice findDevice() {
        HashMap<String, UsbDevice> deviceHashMap = ((UsbManager) AppConst.getApplication().getSystemService(Context.USB_SERVICE)).getDeviceList();
        Iterator<UsbDevice> iterator = deviceHashMap.values().iterator();
        while (iterator.hasNext()) {
            UsbDevice device = iterator.next();
            if (equalsUsbDevice(device)) {
                return device;
            }
        }
        return null;
    }

    @Override
    protected AbsOnDeviceStateEvent createOnDeviceStateEvent(int state, int reConnectCount, boolean isError, String errorLog) {
        return new OnNFCModuleStateEvent(state, reConnectCount, isError, errorLog);
    }

    @Override
    protected Task<Void, String, ConnectTaskResult> createConnectTask() {
        UsbCDCConnectTask usbCDCConnectTask = new UsbCDCConnectTask(this) {
            @Override
            protected UsbDevice filterDevice() {
                return findDevice();
            }
        };
        usbCDCConnectTask.setWatiDevice(true);
        return usbCDCConnectTask;
    }




}
